import entity.Employee;
import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.*;

/**
 * 一、方法引用：若 Lambda 体中的功能，已经有方法提供了实现，可以使用方法引用
 * 			  （可以将方法引用理解为 Lambda 表达式的另外一种表现形式）
 *
 * 1. 对象的引用 :: 实例方法名
 *
 * 2. 类名 :: 静态方法名
 *
 * 3. 类名 :: 实例方法名
 *
 * 注意：
 * 	 ①方法引用所引用的方法的参数列表与返回值类型，需要与函数式接口中抽象方法的参数列表和返回值类型保持一致！
 * 	 ②若Lambda 的参数列表的第一个参数，是实例方法的调用者，第二个参数(或无参)是实例方法的参数时，格式： ClassName::MethodName
 *
 * 二、构造器引用 :构造器的参数列表，需要与函数式接口中参数列表保持一致！
 *
 * 1. 类名 :: new
 *
 * 三、数组引用
 *
 * 	类型[] :: new;
 *
 *
 */
public class TestReference01 {
    @Test
    public void test01() {
        PrintStream printStream = System.out;
        Consumer<String> consumer = (str) -> printStream.println(str);
        consumer.accept("hello u");

        Consumer<String> consumer1 = printStream::println;
        consumer1.accept("hello j");

        Consumer<String> consumer2 = System.out::println;
    }

    // 对象的引用::实例方法名
    @Test
    public void test02() {
        Employee employee = new Employee(101, "张三", 18, 9999.99);
        Supplier<String> supplier = () -> employee.getName();
        System.out.println(supplier.get());

        Supplier<Integer> supplier1 = employee::getAge;
        System.out.println(supplier1.get());
    }

    // 类名::静态方法
    @Test
    public void test03() {
        BiFunction<Double, Double, Double> biFunction = (x, y) -> Math.max(x, y);
        System.out.println(biFunction.apply(10000.6, 9.5));

        BiFunction<Double, Double, Double> biFunction1 = Math::max;
        System.out.println(biFunction1.apply(9.5, 10000.7));
    }

    @Test
    public void test04() {
        Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);
        Comparator<Integer> comparator1 = Integer::compare;
    }

    // 类名::实例方法名
    @Test
    public void test05() {
        BiPredicate<String, String> biPredicate = (x, y) -> x.equals(y);
        System.out.println(biPredicate.test("asd", "asd"));

        BiPredicate<String, String> biPredicate1 = String::equals;
        System.out.println(biPredicate1.test("asd", "asdf"));
    }

    // 构造器引用
    @Test
    public void test06() {
        Supplier<Employee> supplier = () -> new Employee();
        System.out.println(supplier.get());

        Supplier<Employee> supplier1 = Employee::new;
        System.out.println(supplier1.get());

        System.out.println("---------------------");

        Function<String, Employee> function = Employee::new;
        System.out.println(function.apply("vincent zee"));
        BiFunction<String, Integer, Employee> function1 = Employee::new;
        System.out.println(function1.apply("vincentzee", 18));
    }

    // 数组引用
    @Test
    public void test07(){
        Function<Integer,String[]> function = (args)->new String[args];
        System.out.println(function.apply(10).length);

        Function<Integer,Employee[]> function1 = Employee[]::new;
        Employee[] employees = function1.apply(10);
        System.out.println(employees.length);
    }
}
